﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Square.Modify
{
    public class PresenterModifySquare
    {
        
        IModifySquareBasicView viewRegisterBasic;
        Model.Square modifySquare;
        ICollection<Model.Allotment> modifyAllotments = new List<Model.Allotment>();
        string ubicationCroquisSquare;
        IModifySquareView view;
        public PresenterModifySquare(IModifySquareView View, Model.Square square)
        {
            view = View;
            modifySquare = ManagerSquare.GetSquareById(square);
            modifyAllotments = ManagerAllotment.GetAllotmentsBySquare(square);
            ubicationCroquisSquare = ManagerSquare.GetUbicationImage(square);
        }

        public void GoStepTwo()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyAllotments = view.ModifyBasic.AllotmentsToModify;
            modifySquare = view.ModifyBasic.SquareToModify;
            ubicationCroquisSquare = view.ModifyBasic.UbicationCroquisSquareToModify;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void UpdateSquare()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyAllotments = view.ModifyBasic.AllotmentsToModify;
            modifySquare = view.ModifyBasic.SquareToModify;
            ubicationCroquisSquare = view.ModifyBasic.UbicationCroquisSquareToModify;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                modifySquare.Enable = true;
                ManagerSquare.UpdateSquare(modifySquare);
                ManagerAllotment.UpdateAllotmentsInSquare(modifyAllotments,modifySquare);
                ManagerSquare.UpdateImage(ubicationCroquisSquare,modifySquare);
                view.SystemMessage("El Manzano Fue Registrado Exitosamente.!!!!");
                view.RestartSearchSquare();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.Square mp = view.ModifyBasic.SquareToModify;
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        private bool PassSecondStep()
        {

            try
            {
                ICollection<Model.Allotment> allots = view.ModifyBasic.AllotmentsToModify;
                ManagerAllotment.VerifyAllotmentsIsCorrect(allots);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewRegisterBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyAllotments = view.ModifyBasic.AllotmentsToModify;
            modifySquare = view.ModifyBasic.SquareToModify;
            ubicationCroquisSquare = view.ModifyBasic.UbicationCroquisSquareToModify;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {

            //if (viewRegisterBasic != null)
            //{
            //    newAllotments = view.RegisterBasic.AllotmentsToRegister;
            //    newSquare = view.RegisterBasic.SquareToRegister;
            //    ubicationCroquisSquare = view.RegisterBasic.UbicationCroquisSquareToRegister;
            //}
            viewRegisterBasic = view.ModifyBasic;
            view.ModifyBasic.AllotmentsToModify=modifyAllotments;
            view.ModifyBasic.SquareToModify=modifySquare;
            view.ModifyBasic.UbicationCroquisSquareToModify=ubicationCroquisSquare;
            viewRegisterBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
